package com.packet.controller;

import com.packet.mapper.UserLoginMapper;
import com.packet.pojo.Staff;
import com.packet.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @className:LoginController
 * @Author:wallet_packet Date:2023-11-12 10:38
 * @Describe:
 */
@Controller
public class LoginController {

    @Autowired
    UserLoginService userLoginService;

    @RequestMapping("/manager/user/login")
    public String login(@RequestParam("sId") String sId,
                        @RequestParam("sPassword") String sPassword,
                        @RequestParam("duties") String duties,
                        Model model, HttpSession session, HttpServletResponse response) throws IOException {
        Staff staff = userLoginService.selectById(Integer.parseInt(sId));
        if(staff.getSPassword().equals(sPassword)&&staff.getSDuties().equals(duties)){
            session.setAttribute("staff",staff);
            session.setAttribute("loginUser",staff.getSName());
            session.setAttribute("sid",staff.getSId());
            //return "redirect:/toIndex";
            response.sendRedirect("/managerMain");
            return null;
        } else {
            model.addAttribute("msg","职务或账号密码错误");
            return "index";
        }
    }

    @RequestMapping("/judge")
    public String judge(HttpSession session){
        Staff staff = (Staff)session.getAttribute("staff");
        if("经理".equals(staff.getSDuties())||"总经理".equals(staff.getSDuties())){
            return "redirect:/managerMain";
        }
        else if("员工".equals(staff.getSDuties())){
            return "#";
        }
        else if("财务".equals(staff.getSDuties())){
            return "#";
        }
        else if("出纳".equals(staff.getSDuties())){
            return "#";

        }
        return "#";
    }
    @RequestMapping("/manager/user/logOut")
    public String logOut(){
        //session.invalidate();
        return "index";
    }


}
